<template>
    <view class="page-wrap">
        <view style="margin-bottom: 20rpx">
            <Position :info="workDetail" :type="type" :remark="remark" @changeRemark="changeRemark"></Position>
        </view>

        <MovePosition :type="type" :info="moveInfo" :moveOptions="moveOptions" @changeIndex="changeIndex"></MovePosition>

        <view class="content">
            <view class="left hiddenScroll">
                <view class="item" @tap="handClick(index)" :class="{ active: index == current }"
                    v-for="(item, index) in list" :key="index">
                    <view class="txt">{{ item.title }}</view>
                </view>
            </view>
            <view class="right" :class="[isDetail ? 'nopadding' : '']">
                <basePoint :uploadParams="uploadParams" :defaultValue="defaultValue" :intoIndex="intoIndex" :list="list"
                    @getForm="getForm"></basePoint>
            </view>
        </view>

        <view class="btns" v-if="!isDetail">
            <view class="btn save" @tap="submit(1)">存草稿</view>
            <view class="btn sub" @tap="submit(0)">确认提交</view>
        </view>
    </view>
</template>

<script>

import basePoint from './components/basePoint/index.vue'
import { patrolAdd, getWorkDetail, getWorkInfo, getMoveList } from '@/api'
import MovePosition from '@/components/MovePosition'
export default {
    components: { basePoint, MovePosition },
    computed: {
        list() {
            return this.options
        },
        workType() {
            let map = {
                network: "网点",
                safe: "安防",
                basePoint: "基站",
            }
            return map[this.type]
        },
        moveInfo() {
            let item = this.moveOptions.find(v => v.id == this.mid_new)
            if (item) return item
            return {}
        },
    },
    data() {
        return {
            isDetail: '',
            current: '0',
            intoIndex: '',
            form: null,
            modeId: '',
            options: [
                {
                    title: '原基站情况',
                    key: '原基站情况',
                    needFile: true,
                    fileList: [],
                },

                {
                    title: '待装基站情况',
                    key: '待装基站情况',
                    needFile: true,
                    fileList: [],
                },

                {
                    title: '拆迁费用',
                    key: '拆迁费用',
                    needFile: true,
                    fileList: [],
                },

                {
                    title: '拟施工时间',
                    key: '拟施工时间',
                    needFile: true,
                    fileList: [],
                },
                {
                    title: '施工联系人及电话',
                    key: '施工联系人及电话',
                    needFile: true,
                    fileList: [],
                },
                {
                    title: '费用结算时间与方式',
                    key: '费用结算时间与方式',
                    needFile: true,
                    fileList: [],
                },
            ],
            remark: '',
            uploadParams: {},
            workInfo: null,
            workDetail: null,
            type: '1',
            defaultValue: null,
            moveOptions: [],
            mid_new: '',
        }
    },
    async onLoad(options) {
        console.log(options)
        const { type, id, pid, mid, isDetail } = options
        this.type = type
        this.isDetail = isDetail
        this.workInfo = {
            type, id, pid, mid
        }
        this.uploadParams = {
            type, id, pid, mid,
            wid: this.wid
        }
        await this.getMoveList({
            wid: this.wid,
            type,
            vague: ''
        })
        await this.getWorkInfo(this.workInfo)
        await this.getWorkDetail(this.workInfo)
    },
    onShow(){
        this.getWorkInfo(this.workInfo) 
    },
    methods: {
        changeIndex(index) {
            if (this.moveOptions.length > 0) {
                this.mid_new = this.moveOptions[index].id
            }
        },
        async getMoveList(params) {
            let res = await getMoveList(params)
            console.log('move-->', res)
            this.moveOptions = res
        },
        async getWorkDetail(params) {
            if (params.id != '0') {
                let res = await getWorkDetail({
                    id: params.id,
                    wid: this.wid,
                    type: '5'
                })
                if (res.formdata) {
                    this.defaultValue = JSON.parse(res.formdata)
                    this.mid_new = res.mid_new || ''
                    let keys=Object.keys(this.defaultValue)
                    if (this.defaultValue && keys.includes('备注')) {
                        this.remark = this.defaultValue['备注'] || '';
                        delete this.defaultValue['备注']
                    }
                }
            }
        },
        async getWorkInfo(params) {
            let res = await getWorkInfo({
                wid: this.wid,
                mid: params.mid
            })
            this.workDetail = res
        },
        handClick(index) {
            this.current = index
            this.intoIndex = 'slot' + index
        },
        getForm(val) {
            this.form = val;
        },
        submit(draft = 0) {
            if (!this.mid_new) {
                return wx.showToast({
                    title: '请选择拆迁地址',
                    icon: "none"
                })
            }
            wx.showModal({
                title: '提示',
                content: '是否确定提交全部工单和附件图片？',
                success: async (res) => {
                    if (res.confirm) {
                        console.log('用户点击确定', JSON.stringify(this.form))
                        let params = {
                            ...this.workInfo,
                            ac: 'migrate_add',
                            formdata: JSON.stringify({
                                ...this.form,
                                '备注': this.remark
                            }),
                            wid: this.wid,
                            draft,
                            mid_new: this.mid_new
                        }
                        if (params.id == '0') {
                            delete params.id
                        }
                       
                        await patrolAdd(params)
                        wx.showToast({
                            title: draft ? '保存至草稿成功' : '工单提交成功'
                        })
                        setTimeout(() => {
                            wx.navigateBack()
                        }, 1000)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    }
}
</script>

<style src="./assets/index.scss" lang="scss" scoped></style>


